package com.efast.cafe.portal.controller.common;

import java.util.List;
import java.util.Map;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.controller.AbstractController;
import com.efast.cafe.framework.enums.ResultStatus;
import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.cafe.portal.entity.common.PortalTagMap;
import com.efast.cafe.portal.service.common.IPortalTagService;
@Controller
public class PortalTagController extends AbstractController{
	@Autowired
	private IPortalTagService portalTagService;
	
	
	@RequestMapping(value = "/frame_center/common/myTagsCount")
	public @ResponseBody ResultBean queryMyTagsCount(@RequestBody Map map){
		ResultBean res = new ResultBean(true,null);
		boolean flag = false;
		String referenceId = null;
		if(map.get("flag")!=null){
			flag = (boolean)map.get("flag");
		}
		if(map.get("referenceId")!=null){
			referenceId = map.get("referenceId").toString();
		}
		try{
			res.setObject(portalTagService.queryTagsCount(flag,referenceId));
		}catch(Exception e){
			e.printStackTrace();
			res.setSuccess(false);
			res.setErrorCode(ResultStatus.SYS_ERROR.getCode());
		}
		return res;
	}
	
	
	@RequestMapping(value = "/frame_center/common/myTagsEntityCount")
	public @ResponseBody ResultBean queryTagsMapCount(@RequestBody Map<String,String> map){
		ResultBean res = new ResultBean(true,null);
		try{
			if(map.containsKey("tagName") && map.get("tagName")!=null){
				res.setObject(portalTagService.queryTagsMapCount(map.get("tagName")));
			}else{
				res.setSuccess(false);
				res.setErrorCode(ResultStatus.PARAM_ERROR.getCode());
			}
		}catch(Exception e){
			e.printStackTrace();
			res.setSuccess(false);
			res.setErrorCode(ResultStatus.SYS_ERROR.getCode());
		}
		return res;
	}
	
	
	@RequestMapping(value = "/frame_center/common/myTagsEntityDetail")
	public @ResponseBody ResultBean queryMyTagsEntityDetail(@RequestBody Map<String,List<String>> map){
		ResultBean res = new ResultBean(true,null);
		try{
			if(map.containsKey("tagName") && map.get("tagName")!=null){
				res.setObject(portalTagService.queryTagsMapDetail(map.get("tagName"), null));
			}else{
				res.setSuccess(false);
				res.setErrorCode(ResultStatus.PARAM_ERROR.getCode());
			}
		}catch(Exception e){
			e.printStackTrace();
			res.setSuccess(false);
			res.setErrorCode(ResultStatus.SYS_ERROR.getCode());
		}
		return res;
	}
	
	
	@RequestMapping(value = "/frame_center/common/myTagsByEntity")
	public @ResponseBody ResultBean queryTagByEntity(@RequestBody Map map){
		ResultBean res = new ResultBean(true,null);
		try{
			if(map.containsKey("entityName") && map.get("entityName")!=null && map.containsKey("entityId")&& map.get("entityId")!=null){
				res.setObject(portalTagService.queryTagByEntity(map.get("entityName").toString(), Integer.parseInt(map.get("entityId").toString())));
			}else{
				res.setSuccess(false);
				res.setErrorCode(ResultStatus.PARAM_ERROR.getCode());
			}
		}catch(Exception e){
			e.printStackTrace();
			res.setSuccess(false);
			res.setErrorCode(ResultStatus.SYS_ERROR.getCode());
		}
		return res;
	}
	
	@RequestMapping(value = "/frame_center/common/insertTags")
	public @ResponseBody ResultBean insterTags(@RequestBody Map<String,String> map){
		ResultBean res = new ResultBean(true,null);
		try{
			Integer id = null;
			if(map.get("componentName")!=null && map.get("pageTitle")!=null&&map.get("paramJson")!=null&&map.get("referenceId")!=null){
				PortalTagMap portalTagMap = new PortalTagMap();
				portalTagMap.setEntityId(Integer.parseInt(map.get("entityId")!=null?map.get("entityId").toString():"0"));
				portalTagMap.setEntityName(map.get("entityName")!=null?map.get("entityName").toString():"");
				portalTagMap.setPageTitle(map.get("pageTitle").toString());
				portalTagMap.setTagName(map.get("tagName")!=null?map.get("tagName").toString():"");
				portalTagMap.setComponentName(map.get("componentName").toString());
				portalTagMap.setParamJson(map.get("paramJson").toString());
				portalTagMap.setReferenceId(map.get("referenceId").toString());
				portalTagMap.setCompanyCode(SpringWebUtils.getRequestCompany());
				portalTagMap.setUsername(SpringWebUtils.getRequestUser());
				id = portalTagService.insertTags(portalTagMap);
				res.setObject(id.toString());
			}else{
				res.setSuccess(false);
				res.setErrorCode(ResultStatus.PARAM_ERROR.getCode());
			}
		}catch(Exception e){
			e.printStackTrace();
			res.setSuccess(false);
			res.setErrorCode(ResultStatus.SYS_ERROR.getCode());
		}
		return res;
	}
	
	@RequestMapping(value = "/frame_center/common/delTags")
	public @ResponseBody ResultBean delTags(@RequestBody Map<String,String> map){
		ResultBean res = new ResultBean(true,null);
		try{
			if(map.containsKey("tagId") && map.get("tagId")!=null ){
				portalTagService.delTags(Integer.parseInt(map.get("tagId").toString()));
			}else{
				res.setSuccess(false);
				res.setErrorCode(ResultStatus.PARAM_ERROR.getCode());
			}
		}catch(Exception e){
			e.printStackTrace();
			res.setSuccess(false);
			res.setErrorCode(ResultStatus.SYS_ERROR.getCode());
		}
		return res;
	}
	
	/**
	 * 我的tags
	 * @Date        :      2017年9月25日
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/frame_center/common/queryTagsListCount")
	public @ResponseBody ResultBean queryTagsListCount(@RequestBody Map<String,Boolean> map){
		ResultBean res = new ResultBean(true,null);
		boolean flag = false;
		if(map.containsKey("flag")){
			flag = map.get("flag");
		}
		try{
			res.setObject(portalTagService.queryTagsListCount(flag));
		}catch(Exception e){
			e.printStackTrace();
			res.setSuccess(false);
			res.setErrorCode(ResultStatus.SYS_ERROR.getCode());
		}
		return res;
	}
}
